<template>
  <!-- 引入container布局 -->
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header class="el-header">
      <div>
        <img src="../assets/logo.png" alt />
        <span class="headspan">滑雪管理系统:{{role}}</span>
      </div>
      <el-button type="info" @click="logout">安全退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '60px' : '200px'">
        <div class="toggle-button" @click="toggleCollapase">|||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.path"
            v-for="item in menulist"
            :key="item.id"
          >
            <template #title>
              <i :class="iconsObject[item.id]"></i>
              <span>{{ item.title }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="it.path"
              v-for="it in item.sList"
              :key="it.id"
              @click="saveNavState(it.path)"
            >
              <template #title>
                <i :class="iconsObject[it.id]"></i>
                <span>{{ it.title }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主题内容 -->
      <el-main>
        <!-- 路由route children里传来的模块 -->
        <router-view> </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 菜单列表
      menulist: [],
      isCollapse: false, //伸缩
      iconsObject: {
        100: "iconfont icon-guanliyuan_jiaoseguanli",
        200: "iconfont icon-yundong",
        101: "iconfont icon-denglu",
        102: "iconfont icon-mima",
        103: "iconfont icon-yundong",
        104: "iconfont icon-shangpin",
        201: "iconfont icon-tushu",
        202: "iconfont icon-qialuli",
        203: "iconfont icon-sharpicons_burger-",
        204: "iconfont icon-denglu",
      },
      activePath: "/welcome", //默认路径
      role:"",
    };
  },
  created() {
    //查询menulist
    this.getMenuList();
    this.user = window.sessionStorage.getItem("user")
    this.role =  window.sessionStorage.getItem("role")
    this.activePath = window.sessionStorage.getItem("activePath"); //取出session里的path 动态修改activePath
  },
  methods: {
    logout() {
      this.$router.push("/login"); // 回到首页
      window.localStorage.clear();
      sessionStorage.clear();
    },
    //获取导航菜单方法
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      console.log(res);
      if (res.status != 200) return this.$message.error("获取列表失败"); //访问失败
      this.menulist = res.menus; //访问成功的数据回填
    },
    //控制伸缩
    toggleCollapase() {
      this.isCollapse = !this.isCollapse;
    },
    //保存路径
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath); //存放在session里
      this.activePath = activePath;
    },
  },
};
</script>
<style lang="less" scoped>
/* //布局器样式 */
.home-container {
  height: 100%;
}
/* 头部样式 */
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between; /*左右贴边*/
  padding-left: 0%; /*左边界*/
  color: #fff;
  align-items: center;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

/* 侧边样式 */
.el-aside {
  .el-menu {
    border-right: none;
  }
  background-color: #333744;
}
/* 主体样式 */
.el-main {
  background-color: #eaedf1;
}
img {
  width: 55px;
  height: 55px;
}
//按钮样式
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer; //经过变成小手
}
</style>
